<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsBatchLog extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['b.goods_code'] = ['like','%'.$goods_code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['b.manufacture'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $batch_name = $this->request->post('batch_name');
            if(!empty($batch_name)){
                $where['b.batch_name'] = ["like", '%'.$batch_name.'%'];
            }
            $purchase_code = $this->request->post('purchase_code');
            if(!empty($purchase_code)){
                $where['gbl.purchase_code'] = ["like", '%'.$purchase_code.'%'];
            }
            $business_type = $this->request->post('business_type');
            if($business_type!=null && $business_type!==''){
                $where['gbl.business_type'] = $business_type;
            }
            $sale_id = $this->request->post('sale_id');
            if(!empty($sale_id)){
                $where['gbl.sale_id'] = $sale_id;
            }
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['b.goods_code|b.batch_name|gbl.purchase_code'] = ["like", '%'.$keyword.'%'];
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }
            if($pageStatus){
                $list = Db::name('kuyou_goods_batch_log')->alias('gbl')
                    ->join('kuyou_goods g','g.id=gbl.goods_id')
                    ->join('kuyou_unit u','u.id=gbl.goods_unit')
                    ->join('kuyou_stores s','s.id=gbl.stores_id')
                    ->join('kuyou_goods_batch b','b.id=gbl.batch_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->field('gbl.goods_id,gbl.unit_price,gbl.tax_included,gbl.all_money,gbl.tax_money,gbl.money_tax_price,gbl.goods_unit,gbl.stores_id,
                    gbl.batch_id,gbl.number,gbl.purchase_code,gbl.purchase_time,gbl.business_type,gbl.customer_id,gbl.sale_id,gbl.remarks,gbl.order_id,
                    g.goods_code,g.goods_name,g.goods_image,
                    u.name as unit_name,s.name as stores_name,
                    b.batch_name,FROM_UNIXTIME(b.manufacture,\'%Y-%m-%d\') as manufacture,b.guarantee,FROM_UNIXTIME(b.validity,\'%Y-%m-%d\') as validity,b.address,b.registration,b.production')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_batch_log')->alias('gbl')
                    ->join('kuyou_goods g','g.id=gbl.goods_id')
                    ->join('kuyou_unit u','u.id=gbl.goods_unit')
                    ->join('kuyou_stores s','s.id=gbl.stores_id')
                    ->join('kuyou_goods_batch b','b.id=gbl.batch_id')
                    ->where($where)
                    ->field('gbl.goods_id,gbl.unit_price,gbl.tax_included,gbl.all_money,gbl.tax_money,gbl.money_tax_price,gbl.goods_unit,gbl.stores_id,
                    gbl.batch_id,gbl.number,gbl.purchase_code,gbl.purchase_time,gbl.business_type,gbl.customer_id,gbl.sale_id,gbl.remarks,gbl.order_id,
                    g.goods_code,g.goods_name,g.goods_image,
                    u.name as unit_name,s.name as stores_name,
                    b.batch_name,FROM_UNIXTIME(b.manufacture,\'%Y-%m-%d\') as manufacture,b.guarantee,FROM_UNIXTIME(b.validity,\'%Y-%m-%d\') as validity,b.address,b.registration,b.production')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_batch_log')->alias('gbl')
                ->join('kuyou_goods g','g.id=gbl.goods_id')
                ->join('kuyou_unit u','u.id=gbl.goods_unit')
                ->join('kuyou_stores s','s.id=gbl.stores_id')
                ->join('kuyou_goods_batch b','b.id=gbl.batch_id')
                ->where($where)->count();

            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal  = $setList['price_decimal']['value'];

            foreach ($list as $list_k=>$list_v){
                //供应商/客户
                if($list_v['business_type']==2 || $list_v['business_type']==3 || $list_v['business_type']==7 || $list_v['business_type']==8){
                    $list[$list_k]['customer_id'] = Db::name('kuyou_suppliers')->where(['id'=>$list_v['customer_id']])->value('supplyer_name');
                }else if($list_v['business_type']==4 || $list_v['business_type']==5 || $list_v['business_type']==9 || $list_v['business_type']==10){
                    $list[$list_k]['customer_id'] = Db::name('kuyou_customers')->where(['id'=>$list_v['customer_id']])->value('customer_name');
                }
                //单位信息
                $goods = Db::name('kuyou_goods')->where(['id'=>$list_v['goods_id']])->field('more_unit,unit_id')->find();
                $unit = Db::name('kuyou_unit')->where(['id'=>$goods['unit_id']])->field('id,name')->find();
                $unit_name = Db::name('kuyou_unit')->where(['id'=>$list_v['goods_unit']])->value('name');
                $list[$list_k]['unit_name'] = $unit_name;
                $max_unit = 1;
                $unit_arr = [];
                $more_unit_num = $list_v['number'];
                $sub_unit = '';
                if($unit){
                    $unit_arr[] = ['name'=>$unit['name'],'advance_rate'=>1];
                    if($goods['more_unit']==1){
                        $more_unit = Db::name('kuyou_unit_conversion')->alias('uc')
                            ->join('kuyou_unit u','u.id=uc.vice_unit')
                            ->where(['uc.main_unit'=>$unit['id']])
                            ->order('uc.advance_rate','ASC')
                            ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                            ->select();
                        foreach ($more_unit as $unit_k=>$unit_v){
                            $unit_arr[] = ['name'=>$unit_v['name'],'advance_rate'=>$unit_v['advance_rate']];
                            if($unit_v['advance_rate']>$max_unit){
                                $max_unit = $unit_v['advance_rate'];
                            }
                            if($unit_v['id']==$list_v['goods_unit']){
                                $more_unit_num = round(($list_v['number']*$unit_v['advance_rate']),$number_decimal);
                            }
                            $sub_unit = round(($more_unit_num/$unit_v['advance_rate']),$number_decimal).$unit_v['name'];
                        }
                    }
                    $unitconversion = '';
                    $unit_arr = (new Commons())->arraySequence($unit_arr,'advance_rate','SORT_DESC');
                    foreach ($unit_arr as $unit_arr_k=>$unit_arr_v){
                        if(!empty($unitconversion)){
                            $unitconversion .= '=';
                        }
                        $unitconversion .= round(($max_unit/$unit_arr_v['advance_rate']),$number_decimal).$unit_arr_v['name'];
                    }
                    $list[$list_k]['unitconversion'] = $unitconversion;
                    $list[$list_k]['more_unit_num']  = $more_unit_num;
                    $list[$list_k]['sub_unit']  = $sub_unit;
                }
            }
            $result = array("count" => $row_count, "list" => $list);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
